<%@page import="hibari.client.model.DownloadToken"%>
<%@page import="hibari.PMF"%>
<%@page import="com.google.appengine.api.datastore.Entity"%>
<%@page import="com.google.appengine.api.datastore.Query.FilterOperator"%>
<%@page import="com.google.appengine.api.datastore.Query"%>
<%@page
	import="com.google.appengine.api.datastore.DatastoreServiceFactory"%>
<%@page import="com.google.appengine.api.datastore.DatastoreService"%>
<%@page import="hibari.server.mail.Mailer"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	String message = "";
    String tokenId = request.getParameter("id");

    if (tokenId != null) {
        try {
            DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
            Query query = new Query("DownloadToken");
            query.addFilter("uuid", FilterOperator.EQUAL, tokenId);
            Entity e = ds.prepare(query).asSingleEntity();

            DownloadToken token = PMF.get().getPersistenceManager().getObjectById(DownloadToken.class, e.getKey());
            new Mailer().sendMail(token);
			message ="A mail has been sent to " + token.getEmail();
        } catch (Exception e) {
            message = e.getLocalizedMessage();
            e.printStackTrace(response.getWriter());
        }

    }
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hibari Dashboard</title>
<script type="text/javascript" src="jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="common.js"></script>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
	<%=message %>
	<form action="mailtest.jsp" method="get">
		download token id : <input name="id"> <input type="submit">
	</form>
</body>
</html>